package com.ruoyi.sales.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.domain.entity.ZcProcess;
import com.ruoyi.sales.service.IZcProcessService;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 产品类别工序Controller
 */
@RestController
@RequestMapping("/sales/process")
public class ZcProcessController extends BaseController {
    @Autowired
    private IZcProcessService zcProcessService;

    /**
     * 获取列表
     */
    @PreAuthorize("@ss.hasPermi('sales:process:list')")
    @GetMapping("/list")
    public AjaxResult list(ZcProcess process)
    {
        List<ZcProcess> processes = zcProcessService.selectProcessList(process);
        return success(processes);
    }

    /**
     * 查询列表（排除节点）
     * springmvc控制器方法 用于处理ajax请求
     */
    @PreAuthorize("@ss.hasPermi('sales:process:list')")
    @GetMapping("/list/exclude/{processId}")
    public AjaxResult excludeChild(@PathVariable(value = "processId", required = false) Long processId)
    {
        List<ZcProcess> processes = zcProcessService.selectProcessList(new ZcProcess());
        processes.removeIf(p -> p.getProcessId().intValue() == processId || ArrayUtils.contains(StringUtils.split(p.getAncestors(), ","), processId + ""));
        return success(processes);
    }

    /**
     * 根据编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('sales:process:query')")
    @GetMapping(value = "/{processId}")
    public AjaxResult getInfo(@PathVariable Long processId)
    {
        return success(zcProcessService.selectProcessById(processId));
    }

    /**
     * 新增
     */
    @PreAuthorize("@ss.hasPermi('sales:process:add')")
    @Log(title = "类别工序管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody ZcProcess process)
    {
        if (!zcProcessService.checkProcessNameUnique(process))
        {
            return error("新增类别工序'" + process.getProcessName() + "'失败，类别工序名称已存在");
        }
        return toAjax(zcProcessService.insertProcess(process));
    }

    /**
     * 修改
     */
    @PreAuthorize("@ss.hasPermi('sales:process:edit')")
    @Log(title = "原料品类管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody ZcProcess process)
    {
        Long processId = process.getProcessId();
        if (!zcProcessService.checkProcessNameUnique(process))
    {
        return error("修改原料品类'" + process.getProcessName() + "'失败，原料品类名称已存在");
    }
        else if (process.getParentId().equals(processId))
        {
            return error("修改原料品类'" + process.getProcessName() + "'失败，上级不能是自己");
        }
        else if (StringUtils.equals(UserConstants.DEPT_DISABLE, process.getStatus()) && zcProcessService.selectNormalChildrenProcessById(processId) > 0)
        {
            return error("该原料品类包含未停用的子原料品类！");
        }
        return toAjax(zcProcessService.updateProcess(process));
    }

    /**
     * 删除
     */
    @PreAuthorize("@ss.hasPermi('sales:stock:remove')")
    @Log(title = "原料品类管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{processId}")
    public AjaxResult remove(@PathVariable Long processId)
    {
        if (zcProcessService.hasChildByProcessId(processId))
        {
            return warn("存在下级原料品类,不允许删除");
        }
        return toAjax(zcProcessService.deleteProcessById(processId));
    }


    /**
     * 状态修改
     */
    @PreAuthorize("@ss.hasPermi('sales:stock:edit')")
    @Log(title = "原料品类", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody ZcProcess process)
    {
        Long processId = process.getProcessId();
        if (StringUtils.equals(UserConstants.DEPT_DISABLE, process.getStatus()) && zcProcessService.selectNormalChildrenProcessById(processId) > 0)
        {
            return error("该原料品类包含未停用的子原料品类！");
        }
        return toAjax(zcProcessService.updateProcessStatus(process));
    }
}
